# url ='https://tieba.baidu.com/f?kw=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F&ie=utf-8&pn=50'
# https://tieba.baidu.com/f?kw=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F&ie=utf-8&pn=50
# url编码和解码
'''
kw=英雄联盟&ie=utf-8&pn=50
'''
import urllib
import urllib.request as ur
import urllib.parse as up

kw = '美剧'
data = {
    'kw':kw ,
    'ie':'utf-8',
    'pn':'50',
}
# urlencode的参数是词典，它可以将key-value这样的键值对转换成我们想要的格式
data_url  = up.urlencode(
    data
)
print(data_url)
# 当urlencode之后的字符串传递过来之后，接受完毕就要解码了——urldecode。urllib提供了unquote()这个函数，可没有urldecode()！
ret = up.unquote(data_url)
print(ret)


request = ur.Request('https://tieba.baidu.com/f?'+data_url)
response = ur.urlopen(request).read()
with open('%s.html'%kw,'wb') as f:
    f.write(response)
